package org.timemanager.view;

import android.content.Context;

/**
 * the file type to back up database
 * @author Dereky
 * 2013-6-13
 *
 */
public abstract class DataExchange {
	public static final String TABLE_NUMBER = "table_number";
	public static final String TABLE_NAME = "table_name";
	public static final String TABLE_ROWS = "rows";
	
	protected String fileType;
	protected DatabaseTool databaseTool;
	
	/**
	 * save the sqlite database to a file in sd card
	 * @return whether saving is successful
	 */
	public abstract boolean save(String filename);
	
	/**
	 * read a file in sd card to restore the sqlite database
	 */
	public abstract boolean restore(String filename);

	public DataExchange(Context context) {
		databaseTool = new DatabaseTool(context);
	}

	public DataExchange(Context context, String fileType) {
		this(context);
		this.fileType = fileType;
	}

	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}
	
	public static String changeCode(String oldString){
		return oldString;
//		try {
//			return new String(oldString.getBytes("ISO-8859-1"),"GBK");
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//			return "";
//		}
		//return EncodingUtils.getString(oldString.getBytes("UTF-16"),"GBK");
	}
	
	public boolean compareStringArray(String [] one, String [] two){
		if(one == two){
			return true;
		}
		if(one == null || two == null){
			return false;
		}
		if(one.length != two.length){
			return false;
		}
		for(int i=0; i<one.length; i++){
			if(!one[i].equals(two[i])){
				return false;
			}
		}
		return true;
	}
	
	public boolean isNumber(String number){
		try {
			Integer.parseInt(number);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}
}
